package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ClothesAdapter;
import flc.ast.bean.IdPhotosBean;
import flc.ast.bean.MyClothesBean;
import flc.ast.databinding.ActivityProfessionalPhotoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class ProfessionalPhotoActivity extends BaseAc<ActivityProfessionalPhotoBinding> {

    public static Bitmap sBitmap;
    public static int type = 0;
    private ClothesAdapter manClothesAdapter, womanClothesAdapter;
    private int manPos, womanPos;


    @Override
    protected int onCreate() {
        return R.layout.activity_professional_photo;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProfessionalPhotoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivProfessionalPhotoSave.setOnClickListener(this);
        mDataBinding.tvManClothes.setOnClickListener(this);
        mDataBinding.tvWomanClothes.setOnClickListener(this);

        mDataBinding.rvManClothesList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        manClothesAdapter = new ClothesAdapter();
        mDataBinding.rvManClothesList.setAdapter(manClothesAdapter);
        manClothesAdapter.setOnItemClickListener(this);

        mDataBinding.rvWomanClothesList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        womanClothesAdapter = new ClothesAdapter();
        mDataBinding.rvWomanClothesList.setAdapter(womanClothesAdapter);
        womanClothesAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvManClothes://男士
                clearSelection();
                mDataBinding.tvManClothes.setTextColor(Color.parseColor("#5AD9FF"));
                mDataBinding.rvManClothesList.setVisibility(View.VISIBLE);
                break;
            case R.id.tvWomanClothes://女士
                clearSelection();
                mDataBinding.tvWomanClothes.setTextColor(Color.parseColor("#5AD9FF"));
                mDataBinding.rvWomanClothesList.setVisibility(View.VISIBLE);
                break;
            case R.id.ivProfessionalPhotoSave://保存
                mDataBinding.stickerView.setShowHelpToolFlag(false);
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == manClothesAdapter) {
            manClothesAdapter.getItem(manPos).setSelected(false);
            manPos = position;
            manClothesAdapter.getItem(position).setSelected(true);
            manClothesAdapter.notifyDataSetChanged();

            if (position == 0) {
                mDataBinding.stickerView.clear();
            } else {
                Resources res = getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, manClothesAdapter.getItem(position).getClothesIcon());
                mDataBinding.stickerView.addBitImage(bmp);
            }
        } else if (adapter == womanClothesAdapter) {
            womanClothesAdapter.getItem(womanPos).setSelected(false);
            womanPos = position;
            womanClothesAdapter.getItem(position).setSelected(true);
            womanClothesAdapter.notifyDataSetChanged();

            if (position == 0) {
                mDataBinding.stickerView.clear();
            } else {
                Resources res = getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, womanClothesAdapter.getItem(position).getClothesIcon());
                mDataBinding.stickerView.addBitImage(bmp);
            }
        }
    }

    @Override
    protected void initData() {
        IdPhotosBean bean = Content.sIdPhotosBean;
        mDataBinding.tvPixelW.setText(bean.getPixelW() + getString(R.string.px_text) + " / " + bean.getCyW() + getString(R.string.mm_text));
        mDataBinding.tvPixelH.setText(bean.getPixelH() + getString(R.string.px_text) + " / " + bean.getCyH() + getString(R.string.mm_text));

        mDataBinding.imageViewTouch.setImageBitmap(sBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        //男士服装
        List<MyClothesBean> listManClothes = new ArrayList<>();
        listManClothes.add(new MyClothesBean(R.drawable.wu, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_1, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_2, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_3, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_4, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_5, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_6, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_7, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_8, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_9, false));
        listManClothes.add(new MyClothesBean(R.drawable.man_10, false));
        manClothesAdapter.setList(listManClothes);
        //女士服装
        List<MyClothesBean> listWomanClothes = new ArrayList<>();
        listWomanClothes.add(new MyClothesBean(R.drawable.wu, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_1, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_2, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_3, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_4, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_5, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_6, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_7, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_8, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_9, false));
        listWomanClothes.add(new MyClothesBean(R.drawable.woman_10, false));
        womanClothesAdapter.setList(listWomanClothes);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSave);
                        if (type == 1) {
                            ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        if (type == 0) {
                            Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSave);
                            CompositionPhotoActivity.sBitmap = bitmap;
                            startActivity(CompositionPhotoActivity.class);
                        } else {
                            ToastUtils.showShort(R.string.save_success);
                            startActivity(HomeActivity.class);
                        }
                    }
                });
            }
        }, 1000);
    }

    private void clearSelection() {
        mDataBinding.tvManClothes.setTextColor(Color.parseColor("#868484"));
        mDataBinding.tvWomanClothes.setTextColor(Color.parseColor("#868484"));
        mDataBinding.rvManClothesList.setVisibility(View.GONE);
        mDataBinding.rvWomanClothesList.setVisibility(View.GONE);
    }

}